package com.athuahua.webflux.filer;

import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @Author: huahua
 * @Date: 2024/11/20 0020
 * @Time: 16:38
 * @Description:
 */
@Component
public class MyWebFilter implements WebFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {

        ServerHttpResponse response = exchange.getResponse();
        ServerHttpRequest request = exchange.getRequest();


        System.out.println("请求处理放行目标方法前");
        //放行 mono是异步的
        Mono<Void> filter = chain
                .filter(exchange) //放行
                .doOnError(err->{

                    System.out.println("目标方法异常");
                })
                .doFinally(signalType -> {


                    System.out.println("目标方法执行之后");
                });

        /**
         * 注意
         * Mono<Void> filter = chain.filter(exchange)
         *
         * Mono<Void> voidMono =  filter.doxxxx() 处理
         *
         * return voidMono //流一旦经过某个操纵会变成新流，一定要返回新流
         */

        return filter;
    }
}
